// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;
import EventManager from "./managers/EventManager";
import AudioManager from "./managers/AudioManager";
@ccclass
export default class LevelChoose extends cc.Component {
    @property(cc.ScrollView)
    scroll: cc.ScrollView = null;
    // LIFE-CYCLE CALLBACKS:
    @property(cc.Node)
    scrollViewContent: cc.Node = null;
    @property(cc.Prefab)
    levelPanel: cc.Prefab = null;
    onLoad () {
        
    }
    private btnEvent(e: cc.Event,data: any) {
        if(data) {
            switch(data) {
                case "-1":
                    cc.director.loadScene("menu");
                    break;

            }
            EventManager.getInstance().dispatchEvent({type: "btnmusic",message: "btnmusic",target: this});
        }
    }
    start () {
        // this.scroll.
        EventManager.getInstance().addEventListener("btnmusic",this.playmusic,this);
        this.addPanel();
        // this.scrollViewContent.height = 
    }
    private playmusic() {
        AudioManager.getInstance().playEffect("music/sound.wav");
    }
    onDestroy(): void {
        EventManager.getInstance().removeEventListener("btnmusic",this.playmusic,this);
    }
    private createLevelPanel(): cc.Node {
        let levelItem: cc.Node = cc.instantiate(this.levelPanel);
        this.scrollViewContent.addChild(levelItem);
        return levelItem;
    }
    private addPanel(): void {
        for(let i = 0; i < 100; i++) {
            let levelNode: cc.Node = this.createLevelPanel();
            levelNode.getChildByName("levelNum").getComponent(cc.Label).string = (i + 1).toString();
            levelNode.x = 0;
            levelNode.y -= i * 138 + 20;
            
        }
        this.scrollViewContent.height = 100 * 138 + 20;
    }
    // update (dt) {}
}
